//
//  CustomNavBarView.m
//  LangSha
//
//  Created by 于洪东 on 16/6/23.
//  Copyright © 2016年 于洪东. All rights reserved.
//

#import "CustomNavBarView.h"


@interface CustomNavBarView ()



@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UIImageView *imgViewBg;
@property (nonatomic, strong) UIButton *btnLeft;
@property (nonatomic, strong) UIButton *btnRight;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *middleView;


@end


@implementation CustomNavBarView

-(instancetype)init{
    if (self = [super init]) {
        NSLog(@"========");
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame: frame]) {
        [self initUI];
//        [self layoutSubviews];
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
    return self;
}

/**
 *  //////////////////
 */
-(void)initUI{
    _bgView = [[UIView alloc]init];
    _bgView.backgroundColor = [UIColor clearColor];
    _bgView.userInteractionEnabled = YES;
    [self addSubview:_bgView];
    
    _imgViewBg = [[UIImageView alloc] init];
    _imgViewBg.backgroundColor = [UIColor purpleColor];
    _imgViewBg.image = [[UIImage imageNamed:@"NaviBar_Bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    _imgViewBg.alpha = 0.98f;
    _imgViewBg.userInteractionEnabled = YES;
    [self addSubview:_imgViewBg];

    

    _btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnLeft.titleLabel.textAlignment = NSTextAlignmentLeft;
    [_btnLeft setTitle:@"返回" forState:UIControlStateNormal];
    [_btnLeft setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btnLeft addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnLeft];
    
    
    _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight setTitle:@"右边" forState:UIControlStateNormal];
    [_btnRight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btnRight addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnRight];
    
    
    _middleView = [[UIView alloc]init];
    _middleView.backgroundColor = [UIColor clearColor];
    _middleView.userInteractionEnabled = YES;
    [self addSubview:_middleView];
    
    _labelTitle = [[UILabel alloc] init];
    _labelTitle.backgroundColor = [UIColor clearColor];
    _labelTitle.textColor = [UIColor blackColor];
    _labelTitle.text = @"你是猪啊";
    _labelTitle.font = [UIFont systemFontOfSize:20];
    _labelTitle.textAlignment = NSTextAlignmentCenter;
    [_middleView addSubview:_labelTitle];
  
}

/**
 *  横屏  导航条高度 32（没有 statusBar）
 *  竖屏  导航条高度 44（statusBar 要加20）
 */
-(void)layoutSubviews{
    NSLog(@">>>>>>>%f",self.frame.size.width);
    
    if ([UIScreen mainScreen].bounds.size.width > [UIScreen mainScreen].bounds.size.height) {
        // 这是横屏
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 32);
        _bgView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 32);
        _imgViewBg.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 32);
        _btnLeft.frame = CGRectMake(0, 0, 50, 32);
        _btnRight.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -50, 0, 50, 32);
        _middleView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 -50, 0, 100, 32);
        _labelTitle.frame = CGRectMake(0, 0, _middleView.frame.size.width, _middleView.frame.size.height);
        
    }else{
        // 这是竖屏
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
        _bgView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width,44);
        _imgViewBg.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64);
        _btnLeft.frame = CGRectMake(0, 20, 50, 44);
        _btnRight.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -50, 20, 50, 44);
        _middleView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 -50, 20, 100, 44);
        _labelTitle.frame = CGRectMake(0, 0, _middleView.frame.size.width, _middleView.frame.size.height);
    }
}

#pragma mark - public method

-(void)setLeftBtn:(UIButton *)btn{
    if (_btnLeft){
        [_btnLeft removeFromSuperview];
        _btnLeft = nil;
    }else{}
    
    _btnLeft = btn;
    if (_btnLeft){
        [self addSubview:_btnLeft];
    }else{}
}

-(void)setRightBtn:(UIButton *)btn{
    if (_btnRight)
    {
        [_btnRight removeFromSuperview];
        _btnRight = nil;
    }else{}
    
    _btnRight = btn;
    if (_btnRight)
    {
        [self addSubview:_btnRight];
    }else{}
}

-(void)setTitle:(NSString *)strTitle{
    [_labelTitle setText:strTitle];
}


- (void)showCoverView:(UIView *)view{
    [self hideOriginalBarItem:YES];
    if (_bgView){
        [_bgView removeFromSuperview];
    }else{}
    _bgView = view;
    if (_bgView){
        [self addSubview:_bgView];
    }else{}

}

- (void)showCoverViewOnTitleView:(UIView *)view{
    
    if (_middleView) {
        [_middleView removeFromSuperview];
    }else{}
    _middleView = view;
    if (_middleView) {
        [self addSubview:_middleView];
    }
    

}

- (void)hideCoverView:(UIView *)view
{
    [self hideOriginalBarItem:NO];
    if (view && (view.superview == self))
    {
        [view removeFromSuperview];
    }else{}
}

- (void)hideOriginalBarItem:(BOOL)bIsHide{
    if (_bgView) {
        _bgView.hidden = bIsHide;
    }else{}
    
    if (_btnLeft){
        _btnLeft.hidden = bIsHide;
    }else{}
    
    if (_btnRight){
        _btnRight.hidden = bIsHide;
    }else{}
    
    if (_middleView){
        _middleView.hidden = bIsHide;
    }else{}
    
//    if (_imgViewBg) {
//        _imgViewBg.hidden = bIsHide;
//    }else{}
    
}




#pragma mark - 响应方法
-(void)leftButtonClick:(UIButton *)sender{
    NSLog(@"左边按钮点击");
    if (self.m_viewCtrlParent){

        BOOL popAndPush = false;
        NSArray *viewControllers = self.m_viewCtrlParent.navigationController.viewControllers;
        NSLog(@">>>>>>%@",viewControllers);
        
        if (viewControllers.count>=2) {
            for (UIViewController *viewCtl in viewControllers) {
                if ([viewCtl isEqual:[viewControllers objectAtIndex:viewControllers.count-2]]) {
                    popAndPush = YES;
                }
            }
        }else{
            NSLog(@"这是第一页的返回按钮");
        }
        if (viewControllers.count > 1 && popAndPush == YES) {
            [self.m_viewCtrlParent.navigationController popViewControllerAnimated:YES];
        } else {
            [self.m_viewCtrlParent dismissViewControllerAnimated:YES completion:nil];
        }

    }else{
        NSLog(@"应该是找不到控制器");
    }
    
}

-(void)rightButtonClick:(UIButton *)sender{
    NSLog(@"右边按钮点击");
}





@end
